#include<cstdio>//uncle-lu
#include<vector>
#include<algorithm>
template<class T>void read(T &x)
{
	x=0;int f=0;char ch=getchar();
	while(ch<'0'||ch>'9') { f|=(ch=='-'); ch=getchar(); }
	while(ch<='9'&&ch>='0') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
	x = f ? -x : x;
	return ;
}	

struct node{
	int v;
	long long int val;
};
std::vector<node>edge[100010];
long long int line[100010], cnt[100010], n;
long long int F[100010], G[100010], sum;

void DP(int x, int fa)
{
	cnt[x] = line[x];
	for (int i =  0; i < (int)edge[x].size(); i++) 
	{
		int v = edge[x][i].v;
		if(v == fa)continue;
		DP(v, x);
		G[x] += G[v] + cnt[v] * edge[x][i].val;
		cnt[x] += cnt[v];
	}
	return ;
}

void DFS(int x, int fa)
{
	for (int i = 0; i < (int)edge[x].size(); i++) 
	{
		int v = edge[x][i].v;
		if(v == fa)continue;

		F[v] = F[x] - cnt[v]*edge[x][i].val + (sum-cnt[v])*edge[x][i].val;

		DFS(v, x);
	}
	return ;
}

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) 
		read(line[i]), sum += line[i];

	int x, y, val;
	for (int i = 1; i <= n-1; i++) 
	{
		read(x);read(y);read(val);
		edge[x].push_back((node){y, val});
		edge[y].push_back((node){x, val});
	}

	DP(1, 0);

	F[1] = G[1];
	
	DFS(1, 0);

	long long int mi = F[1];
	for (int i = 1; i <= n; i++) 
		mi = std::min(F[i], mi);

	printf("%lld", mi);
	return 0;
}
